<?php
namespace App\Http\Controllers\Admin;

use App\Models\SystemSetting;
use Illuminate\Http\Request;
class SettingController extends BaseController
{
    
    /**
     * 
     * @param Request $req
     * @return \Illuminate\View\$this
     */
    public  function getContent() 
    {
        if(!$this->user->can(array('viewBbsContent','viewContent','viewBbsPerm'))) abort(405);
        
        $systemSetting = new SystemSetting();
        if(str_contains(\Illuminate\Support\Facades\Request::getRequestUri(),'content/info'))
        {
            $result = $systemSetting->getSetting("index");
            $viewPath = 'admin.setting.content';
        }
        else
        {
            $result = $systemSetting->getSetting('bbs');
            $viewPath = 'admin.bbs.setting.content';
        }
        return view($viewPath)->with('result',$result);
    }
    
    /**
     * 
     * @param Request $req
     * @return \Illuminate\View\$this
     */
    public function  getPermission(Request $req) 
    {
        if(!$this->user->can('viewBbsPerm')) abort(405);
        
        $systemSetting = new SystemSetting();
        $result = $systemSetting->getSetting('bbs');
        return view('admin.bbs.setting.permission')->with('result',$result);
    }
    
    /**
     * @param Request $req
     */
    public function postContent(Request $req)
    {
        if(!$this->user->can(array('editBbsContent','editContent'))) abort(405);
        
        if(str_contains(\Illuminate\Support\Facades\Request::getRequestUri(),'content/info'))
        {
            $data = array(
                'site_name'=>serialize($req->input('site_name','')),
                'keywords'=>serialize($req->input('keywords','')),
                'description'=>serialize($req->input('description','')),
                'icp_beian'=>serialize($req->input('icp_beian','')),
                'admin_login_seccode'=>serialize($req->input('admin_login_seccode','N')),
                'sensitive_words'=>serialize($req->input('sensitive_words','')),
                'about'=>serialize($req->input('about','')),
                'join'=>serialize($req->input('join','')),
                'contact'=>serialize($req->input('contact',''))
            );
            foreach ($data as $key=>$res)
            {
                SystemSetting::where('varname', $key)
                ->update(['value' => $res]);
            }
            return  redirect()->intended('admin/content/info')->with('success','保存成功！');
        }
        else
        {
            
        }
    }
    
    /**
     * 保存用户权限
     * @param Request $req
     */
    public function postPermission(Request $req)
    {
        if(!$this->user->can('editBbsPerm')) abort(405);
        
        $data = array(
            'answer_unique'=>serialize($req->input('answer_unique','N')),
            'answer_self_question'=>serialize($req->input('answer_self_question','N')),
            'answer_gap'=>serialize($req->input('answer_gap',10)),
            'question_gap'=>serialize($req->input('question_gap',10))
        );
        foreach ($data as $key=>$res)
        {
             SystemSetting::where('varname', $key)
                  ->update(['value' => $res]);     
        }
        return  redirect()->intended('admin/bbs/permission')->with('success','保存成功！');
        
    }
}